package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.DiaryDao;
import com.itheima.entity.PageResult;
import com.itheima.exceptions.BusinessException;
import com.itheima.pojo.Diary;
import com.itheima.pojo.OrderSetting;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service(version = "1.0.0",interfaceClass = DiaryService.class)
@Transactional
public class DiaryServiceImpl implements DiaryService {

    @Resource
    private DiaryDao diaryDao;


    @Override
    public PageResult findPage(int pageNum, int pageSize, String queryString) {
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        //调用dao层查询语句
        Page page = diaryDao.findPage(queryString);
        //封装分页查询结果对象
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Diary> findAll() {
        return diaryDao.findAll();
    }

    @Override
    public void add(Diary diary) {
        Integer count = diaryDao.findByNameOrCode(diary);
        if (count > 0) {
            throw new RuntimeException("已存在被访人或者访问人的检查项，添加失败！");
        }
        diaryDao.add(diary);
    }

    @Override
    public void update(Diary diary) throws BusinessException {
        Integer count = diaryDao.findByNameOrCode(diary);
        if (count > 0) {
            throw new RuntimeException("已存在被访人或者访问人的检查项，编辑失败！");
        }
        diaryDao.update(diary);
    }

    @Override
    public void delete(int id) throws BusinessException {
        diaryDao.delete(id);
    }

    @Override
    public void upload(List<Diary> diarys) throws BusinessException {
        for (Diary diary : diarys) {
            diaryDao.upload(diary);
        }
    }
}